﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Pcb.DomainModel;
using System.Transactions;

namespace Pcb.DAL
{
    public class OrdersDAL
    {
        
        public static List<t_orders> GetOrderList(string[] condition, int pageIndex, int pageSize, out int totalRecord)
        {
            totalRecord = 0;
            List<t_orders> list = new List<t_orders>();

            PcbEntities _context = new PcbEntities();

            try
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    string userName = condition[0];

                    var q = from user in _context.t_orders select user;
                    q = q.OrderBy(a => a.AddDate);  //排序

                    //if (!string.IsNullOrEmpty(userName))
                    //    q = q.Where(a => a.UserName == userName);

                    totalRecord = q.Count();
                    list = (q.Skip((pageIndex - 1) * pageSize).Take(pageSize)).ToList();

                    scope.Complete();   // 提交事务
                }
            }
            catch (Exception ex)
            {
                // 出错处理，并返回一个空对象
                Pcb.Common._Log.SaveMessage(ex.ToString());
                list = new List<t_orders>();
            }

            _context.Dispose();

            return list;
        }

    }
}
